/*

*/
;layui.define(['table', 'jquery', 'form','layer'], function (exports) {
  "use strict";
  var MOD_NAME = 'autoMatch',
      $ = layui.jquery,
      table = layui.table,
      form = layui.form,layer = layui.layer;
  var autoMatch = function () {
      this.v = '1.0.0';
  };

  function Trim(str){return str.replace(/(^\s*)|(\s*$)/g, "");}
  /**
  * 初始化表格选择器
  */
  autoMatch.prototype.render = function (opt) {

      var elem = $(opt.elem);
      var autoMatch_table;
      //默认设置
      opt.keywordKey = opt.keywordKey || 'keywords';
      opt.toolbarevent = opt.toolbarevent || 'selecttr';
      opt.table.page = opt.table.page || true;
      opt.table.height = opt.table.height || 315;
      opt.oldkeywordid = opt.oldkeywordid || 'autoMatch_keywordkey';
      
      var whereobj={};
          whereobj[opt.keywordKey] = Trim(elem.val());
      opt.table.where = whereobj;
      //input propertychange
      elem.on('click keyup', function(e) {
          e.stopPropagation();
          //已经存在
          if($('div.autoMatch').length >= 1){
              //重载表格
              var wh = {};
              wh[opt.keywordKey] = Trim(elem.val());
              autoMatch_table.reload({where: wh,page: {curr: 1}});
              return false;
          }

          var t = elem.offset().top + elem.outerHeight()+"px";
          var l = elem.offset().left +"px";
          var tableName = "autoMatch_table_" + new Date().getTime();
          var tableBox = '<div class="autoMatch layui-anim layui-anim-upbit" style="left:'+l+';top:'+t+';border: 1px solid #d2d2d2;background-color: #fff;box-shadow: 0 2px 4px rgba(0,0,0,.12);padding:10px 10px 0 10px;position: absolute;z-index: 19900001;margin: 5px 0;border-radius: 2px;">';
              tableBox += '<table id="'+tableName+'" lay-filter="'+tableName+'"></table>';
              tableBox += '</div>';
              tableBox = $(tableBox);
          $('body').append(tableBox);

          //渲染TABLE
          opt.table.elem = "#"+tableName;
          autoMatch_table = table.render(opt.table);
          
          //双击行选中
          table.on('rowDouble('+tableName+')', function(obj){
              var checkStatus = {data:[obj.data]};
              opt.done(elem, checkStatus);
              tableBox.remove();
          })

          //监听选中
          table.on('tool('+tableName+')', function(obj){
              if(obj.event === opt.toolbarevent){
                  var checkStatus = {data:[obj.data]};
                  opt.done(elem, checkStatus);
                  tableBox.remove();
              }
          });
          
          //点击其他区域关闭
          $(document).mouseup(function(e){
              var userSet_con = $(''+opt.elem+',.autoMatch');
              if(!userSet_con.is(e.target) && userSet_con.has(e.target).length === 0){
                  tableBox.remove();
              }
          });
      })
  }

  /**
  * 隐藏选择器
  */
  autoMatch.prototype.hide = function (opt) {
      $('.autoMatch').remove();
  }

  //自动完成渲染
  var autoMatch = new autoMatch();

  //FIX 滚动时错位
  $(window).scroll(function () {
      autoMatch.hide();
  });

  exports(MOD_NAME, autoMatch);
})